import { DeleteRequest, GetRequest, PostRequest, PutRequest } from '@/utils/request'
import { doEncrypt } from '@/utils/smHelper'

export type MessageRecordListInfo = {
  homeappliances: MessageRecord[]
  homeappliances_unreadSize: number
  system: MessageRecord[]
  system_unreadSize: number
}
export type MessageRecord = {
  haid: string
  id: number
  isLinkClicked: number
  isRead: number
  isSent: number
  loginid: string
  messageContent: string
  messageEvent: null | string
  messageKey: null | string
  messageLink: string
  messageTitle: string
  messageType: number
  name: string
  photo: string
  pic: string
  readTime: null | string
  sendTime: string
  type: string
  vib: string
}

/**
 * 用户通知信息列表
 */
export function getMessageRecordListInfo(
  type: number,
  page: number,
  pageSize: number,
  isSent: string,
  isRead: string,
  isLinkClicked: string
) {
  return PostRequest<MessageRecordListInfo>('/api/wx/messageRecord/v1/getMessageRecordListInfo', {
    type,
    page,
    pageSize,
    isSent,
    isRead,
    isLinkClicked,
  })
}

/**
 * 阅读单条通知信息（将状态改为已读)
 */
export async function updateMessageRecordReadStatus(id: number) {
  return PostRequest('/api/wx/messageRecord/updateMessageRecordReadStatus', { id })
}

/**
 * 批量阅读通知信息（将未读改为已读)
 */
export async function readMessageRecords() {
  return PostRequest('/api/wx/messageRecord/readMessageRecords')
}

/**
 * 点击通知链接
 */
export async function updateClickLinkStatus(id: number) {
  return PostRequest('/api/wx/messageRecord/updateClickLinkStatus', { id })
}

/**
 * 获取usage report对应月份家电列表
 */
export type UsageReportList = {
  haid: string
}
export async function getUsageReportList() {
  return PostRequest<UsageReportList[]>('/api/usagereport/list')
}

/**
 * 获取家电分享用户信息
 */
export type shareListType = {
  guest: boolean
  loginId: string
  share: ApplianceShareUserType[]
}
export type ApplianceShareUserType = {
  haid: string
  icon: string
  status: string
  acceptTime: string
  haName: string
  guestId: string
  tname: string
}
export async function getApplianceShareUserList(haId: string) {
  return GetRequest<shareListType>(`/api/wx/share/homeAppliance/history/${haId}`)
}

/**
 * 用户是否有未读消息
 */
export type UserIsHaveMessageRemindType = {
  unread: boolean
}
export async function getUserIsHaveMessageRemind() {
  return GetRequest<UserIsHaveMessageRemindType>(`/api/user/message/status`)
}

/**
 * 拒绝分享
 */
export async function declineShare(sharetoken: string) {
  return DeleteRequest(`/api/wx/share/homeAppliance/decline`, { sharetoken }, { noAuth: true, ignoreErrorTips: true })
}

/**
 * 接收分享
 */
export async function acceptShare(sharetoken: string, shareType: string) {
  return PostRequest(
    `/api/wx/share/homeAppliance/v1/accept`,
    { sharetoken, type: shareType },
    { noAuth: true, ignoreErrorTips: true }
  )
}
